package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imufe.chemicalenterprisedangerwarning.entity.po.RolePO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.RoleDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.RoleVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.RoleMapper;
import com.imufe.chemicalenterprisedangerwarning.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.service.UserRoleService;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-10-11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePO> implements RoleService {

    @Autowired
    RoleMapper mapper;

    @Autowired
    UserRoleService userRoleService;

    @Override
    public RoleVO basic(Long id) {
        RolePO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, RoleVO.class);
    }

    @Override
    public RoleDetailedVO detailed(Long id) {
        RolePO po = mapper.selectById(id);
        return BeanUtils.copyPOJO(po, RoleDetailedVO.class);
    }

    @Override
    public IPage<RoleDetailedVO> page(Long current, Long size) {

        Page<RolePO> poPage = new Page<>(current, size);
        mapper.selectPage(poPage, null);
        Page<RoleDetailedVO> voPage = ListUtils.copyPage(poPage, RoleDetailedVO.class);
        List<RolePO> pos = poPage.getRecords();
        voPage.setRecords(ListUtils.copyList(pos, RoleDetailedVO.class));
        return voPage;
    }

    @Override
    public RoleDetailedVO getDetailByUserId(Long userId) {
        Long id = userRoleService.getRoleIdByUserId(userId);
        return detailed(id);
    }
}
